package io.fatmore.web.api;

import java.io.Serializable;
import java.util.List;

/**
 * Created by yanliang on 2017/2/15.
 */
public class ApiOpenResponse implements Serializable {


    private static final long serialVersionUID = -1087483926225165378L;
    private Boolean success ; //旧接口兼容，所以该字段保留
    private int code ;
    private String msg;
    private Object data ;
    private List rows; //旧接口兼容，所以该字段保留

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 返回错误信息
     */
    public static ApiOpenResponse error(String msg){
        ApiOpenResponse response = new ApiOpenResponse();
        response.setSuccess(false);
        response.setCode(ApiResponseCode.error);
        if(msg.isEmpty()){
            msg = "error";
        }
        response.setMsg(msg);
        return response;
    }

    /**
     * 返回成功信息
     */
    public static ApiOpenResponse success(){
        ApiOpenResponse response = new ApiOpenResponse();
        response.setSuccess(true);
        response.setCode(ApiResponseCode.ok);
        response.setMsg("ok");
        return response;
    }
    public static ApiOpenResponse success(String msg){
        ApiOpenResponse response = new ApiOpenResponse();
        response.setSuccess(true);
        response.setCode(ApiResponseCode.ok);
        if(msg.isEmpty()){
            msg = "ok";
        }
        response.setMsg(msg);
        return response;
    }
    public static ApiOpenResponse success(Object data) {
        ApiOpenResponse response = new ApiOpenResponse();
        response.setSuccess(true);
        response.setCode(ApiResponseCode.ok);
        response.setMsg("success");
        response.setData(data);
        return response;
    }

    public static ApiOpenResponse illegal(){
        return illegal(null);
    }
    public static ApiOpenResponse illegal(String message){
        ApiOpenResponse response = new ApiOpenResponse();
        response.setCode(ApiResponseCode.illegal);
        if(message == null){
            message = "parameter illegal";
        }
        response.setMsg(message);
        return response;
    }


    public static ApiOpenResponse rows(List list,String msg){
        ApiOpenResponse response = new ApiOpenResponse();
        response.setSuccess(true);
        response.setCode(ApiResponseCode.ok);
        if(msg.isEmpty()){
            msg = "ok";
        }
        response.setMsg(msg);
        response.setRows(list);
        return response;
    }
}
